package com.codbking.calendar.exaple;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.style.CharacterStyle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.codbking.calendar.exaple.richeditor.ColorPickerDialog;
import com.codbking.calendar.exaple.richeditor.CursorPositionChangeListener;
import com.codbking.calendar.exaple.richeditor.RichEditor;
import com.codbking.calendar.exaple.richeditor.TextStyleFactory;

public class Main3Activity extends Activity {

    private ImageView bold, italic, underLine;
    private RelativeLayout textColor, textBg;
    private View textColorValue, textBgColorValue;
    private Button bullet;
    private RichEditor editText;
    private boolean isBold = false;
    private boolean isItalic = false;
    private boolean isUnderLine = false;
    private boolean isBullet = false;
    private int position = 0;
    private int selectColor = 0xccDCDCDC;
    private String htmlText = "";
    private EditText text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        editText = (RichEditor) findViewById(R.id.rich_editor);
        bold = (ImageView) findViewById(R.id.bold);
        italic = (ImageView) findViewById(R.id.italic);
        underLine = (ImageView) findViewById(R.id.under_line);
        textColor = (RelativeLayout) findViewById(R.id.text_color);
        textBg = (RelativeLayout) findViewById(R.id.text_background);
        bullet = (Button) findViewById(R.id.bullet);
        textColorValue = findViewById(R.id.text_color_value);
        textBgColorValue = findViewById(R.id.text_bg_color_value);
        text = (EditText) findViewById(R.id.text);
        text.setText(HtmlCompat.fromHtml("<b>SUPERVISOR - 10-07-2017 14:14:25</b><br>aaaa<span style=\"font-weight: bold;\">aaaaa</span><span style=\"font-style: italic;font-weight: bold;\">aaaaa</span><span style=\"text-decoration-line: underline;font-style: italic;font-weight: bold;\">aaaaaa</span><span style=\"text-decoration-line: underline;font-style: italic;font-weight: bold;background-color: rgb(255, 228, 181);\">aaaaa</span><span style=\"color: rgb(255, 165, 0);\"><span style=\"text-decoration-line: underline;font-style: italic;font-weight: bold;background-color: rgb(255, 228, 181);\">aaaaaaa</span><span style=\"background-color: rgb(255, 255, 255);\"><span style=\"text-decoration-line: underline;font-style: italic;font-weight: bold;\">aaaaaaaaa</span><span style=\"text-decoration-line: underline;font-style: italic;\">aaaa</span><span style=\"text-decoration-line: underline;\">aaaaa</span>aaaaaaaa</span></span><br><br>\n"));
        final ColorPickerDialog colorPickerDialog = new ColorPickerDialog(Main3Activity.this);
        final ColorPickerDialog bgColorPickerDialog = new ColorPickerDialog(Main3Activity.this);

        bullet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editText.setBullet();
                isBullet = !isBullet;
                bullet.setText("bullet " + isBullet);

//                AlertDialog.Builder builder = new AlertDialog.Builder(
//                        Main3Activity.this);
//                builder.setTitle("提示");
//                builder.setMessage("这是一个普通的对话框！");
//                builder.setCancelable(false);
//                builder.setPositiveButton("知道了！", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        dialog.cancel();
//                    }
//                });
//                builder.create().show();

                colorPickerDialog.setPosition(Main3Activity.this.position);
                colorPickerDialog.show();
                colorPickerDialog.setOnButtonClickListener(new ColorPickerDialog.OnButtonClickListener() {
                    @Override
                    public void onPositiveListener(ColorPickerDialog colorPickerDialog, int color, int position) {
                        Main3Activity.this.position = position;
                        colorPickerDialog.dismiss();
                    }

                    @Override
                    public void onNegativeListener(ColorPickerDialog colorPickerDialog) {
                        colorPickerDialog.dismiss();
                    }
                });
            }


        });

        bold.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                editText.setBold();
                isBold = !isBold;
                if (isBold) {
                    bold.setBackgroundColor(selectColor);
                } else {
                    bold.setBackgroundColor(0xfffffff);
                }
            }
        });
        italic.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                editText.setItalic();
                isItalic = !isItalic;
                if (isItalic) {
                    italic.setBackgroundColor(selectColor);
                } else {
                    italic.setBackgroundColor(0xfffffff);
                }
            }
        });
        underLine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editText.setUnderLine();
                isUnderLine = !isUnderLine;
                if (isUnderLine) {
                    underLine.setBackgroundColor(selectColor);
                } else {
                    underLine.setBackgroundColor(0xfffffff);
                }
            }
        });
        textColor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                colorPickerDialog.setPosition(Main3Activity.this.position);
                colorPickerDialog.show();
                colorPickerDialog.setOnButtonClickListener(new ColorPickerDialog.OnButtonClickListener() {
                    @Override
                    public void onPositiveListener(ColorPickerDialog colorPickerDialog, int color, int position) {
                        Main3Activity.this.position = position;
                        TextStyleFactory.textColor = color;
                        textColorValue.setBackgroundColor(color);
                        colorPickerDialog.dismiss();
                    }

                    @Override
                    public void onNegativeListener(ColorPickerDialog colorPickerDialog) {
                        colorPickerDialog.dismiss();
                    }
                });
            }
        });
        textBg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bgColorPickerDialog.setPosition(Main3Activity.this.position);
                bgColorPickerDialog.show();
                bgColorPickerDialog.setOnButtonClickListener(new ColorPickerDialog.OnButtonClickListener() {
                    @Override
                    public void onPositiveListener(ColorPickerDialog colorPickerDialog, int color, int position) {
                        Main3Activity.this.position = position;
                        TextStyleFactory.textBgColor = color;
                        textBgColorValue.setBackgroundColor(color);
                        colorPickerDialog.dismiss();
                    }

                    @Override
                    public void onNegativeListener(ColorPickerDialog colorPickerDialog) {
                        colorPickerDialog.dismiss();
                    }
                });
            }
        });

        editText.getCurrentEditText().addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.d("aaaaaaaaaaaaaaa ", HtmlCompat.toHtml(editText.getCurrentEditText().getText()));
                htmlText = HtmlCompat.toHtml(editText.getCurrentEditText().getText());
                text.setText(HtmlCompat.fromHtml(htmlText));
                Log.d("aaaaa  ", "currentEditText.getSelectionStart() " + s);
                CharacterStyle[] typeSpans = editText.getCurrentEditText().getEditableText().getSpans(0, s.length(), CharacterStyle.class);
                int next = editText.getCurrentEditText().getEditableText().nextSpanTransition(0, s.length(), CharacterStyle.class);
                Log.d("aaaaa  ", "next " + next);
                for (int i = 0; i < typeSpans.length; i++) {
                    int spanStart = editText.getCurrentEditText().getEditableText().getSpanStart(typeSpans[i]);
                    int spanEnd = editText.getCurrentEditText().getEditableText().getSpanEnd(typeSpans[i]);
                    Log.d("aaaaa  ", "currentEditText.getSelectionStart() " + editText.getCurrentEditText().getSelectionStart()
                            + " spanStart " + spanStart + " spanEnd " + spanEnd + " start " + start + " before " + before);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        editText.setCursorPositionChangeListener(new CursorPositionChangeListener() {
            @Override
            public void setCursorPositionChangeListener(int textStyle) {
                if (textStyle == TextStyleFactory.NORMAL_SPAN) {
                    bullet.setText("bullet false");
                    underLine.setBackgroundColor(0xfffffff);
                    italic.setBackgroundColor(0xfffffff);
                    bold.setBackgroundColor(0xfffffff);
                    textColorValue.setBackgroundColor(0xff000000);
                    textBgColorValue.setBackgroundColor(0xff000000);
                    isBold = false;
                    isItalic = false;
                    isUnderLine = false;
                    isBullet = false;
                } else if (textStyle == TextStyleFactory.BOLD_SPAN) {
                    isBold = true;
                    bold.setBackgroundColor(selectColor);
                } else if (textStyle == TextStyleFactory.ITALIC_SPAN) {
                    isItalic = true;
                    italic.setBackgroundColor(selectColor);
                } else if (textStyle == TextStyleFactory.UNDERLINE_SPAN) {
                    isUnderLine = true;
                    underLine.setBackgroundColor(selectColor);
                } else if (textStyle == TextStyleFactory.FOREGROUND_COLOR_SPAN) {
                    textColorValue.setBackgroundColor(TextStyleFactory.textColor);
                } else if (textStyle == TextStyleFactory.BACKGROUND_COLOR_SPAN) {
                    textBgColorValue.setBackgroundColor(TextStyleFactory.textBgColor);
                } else if (textStyle == TextStyleFactory.BULLET_SPAN) {
                    isBullet = true;
                    bullet.setText("bullet true");
                }
            }
        });
    }
}
